/**
 * 
 */
package org.hotpotmaterial.codegenn2.controller.v1;

import org.hotpotmaterial.codegenn2.common.ResultCodeConstants;
import org.hotpotmaterial.codegenn2.config.bean.PluginMapProperties;
import org.hotpotmaterial.codegenn2.dto.ResultBaseDTO;
import org.hotpotmaterial.codegenn2.dto.ResultOfListDTO;
import org.hotpotmaterial.codegenn2.dto.ResultOfObjectDTO;
import org.hotpotmaterial.codegenn2.dto.ResultOfPluginDTO;
import org.hotpotmaterial.codegenn2.dto.ResultOfPluginPropertiesDTO;
import org.hotpotmaterial.codegenn2.dto.request.StringRequestDTO;
import org.hotpotmaterial.codegenn2.service.IPluginService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;

import io.swagger.annotations.Api;

/**
 * @author Wenxing
 *
 */
@Controller
@RequestMapping("/hmteam/codegen/v1/plugins")
@Api(value = "插件Controller", tags={"插件管理"})
public class PluginController {

	@Autowired
	private PluginMapProperties pluginMapProperties;

	@Autowired
	private IPluginService pluginService;

	/**
	 * 获取所有插件及其属性
	 * @return
	 */
	@GetMapping
	public ResponseEntity<ResultOfPluginDTO> getAllPlugins() {
		ResultOfPluginDTO result = ResultOfPluginDTO.builder().data(pluginMapProperties.getProperties())
				.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build();
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

	/**
	 * 根据插件名称获取插件属性
	 * @param pluginName
	 * @return
	 */
	@GetMapping("/{pluginName}")
	public ResponseEntity<ResultOfPluginPropertiesDTO> getPluginProperties(@PathVariable String pluginName) {
		ResultOfPluginPropertiesDTO result = ResultOfPluginPropertiesDTO.builder()
				.data(pluginService.getPluginProperties(pluginName)).code(ResultCodeConstants.CODE_SUCCESS)
				.msg(ResultCodeConstants.MSG_SUCCESS).build();
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

	/**
	 * 根据插件名称和文件路径判断是否为目录
	 * @param pluginName
	 * @param data
	 * @return
	 */
	@PostMapping("/{pluginName}/templates/is_dir")
	public ResponseEntity<ResultBaseDTO> postPluginTemplatesIsDir(@PathVariable String pluginName,
			@RequestBody StringRequestDTO data) {
		ResultBaseDTO result = ResultOfObjectDTO.builder().data(pluginService.isDirectory(pluginName, data.getParam()))
				.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build();
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

	/**
	 * 根据插件名称和文件路径获取文件夹下面所有文件名称
	 * @param pluginName
	 * @param data
	 * @return
	 */
	@PostMapping("/{pluginName}/templates/dir_files")
	public ResponseEntity<ResultBaseDTO> postPluginTemplatesDirFiles(@PathVariable String pluginName,
			@RequestBody StringRequestDTO data) {
		ResultBaseDTO result = ResultOfListDTO.<String>builder()
				.data(pluginService.getTemplateFileList(pluginName, data.getParam()))
				.code(ResultCodeConstants.CODE_SUCCESS).msg(ResultCodeConstants.MSG_SUCCESS).build();
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

	/**
	 * 根据插件名称和文件路径获取文件内容
	 * @param pluginName
	 * @param data
	 * @return
	 */
	@PostMapping("/{pluginName}/templates/file_content")
	public ResponseEntity<ResultBaseDTO> postPluginTemplatesFileContent(@PathVariable String pluginName,
			@RequestBody StringRequestDTO data) {
		ResultBaseDTO result = ResultOfObjectDTO.builder()
				.data(pluginService.getFileContent(pluginName, data.getParam())).code(ResultCodeConstants.CODE_SUCCESS)
				.msg(ResultCodeConstants.MSG_SUCCESS).build();
		return new ResponseEntity<>(result, HttpStatus.OK);
	}

}
